<template>
  <!--自定义导航栏 -->
  <CustomNavbar />
  <!-- 滚动容器 -->
  <scroll-view
    class="scroll-view"
    scroll-y
    @scrolltolower="onScrolltolower"
    refresher-enabled
    @refresherrefresh="onRefresherrefresh"
    :refresher-triggered="isTriggred"
  >
    <view v-if="isTriggred" class="refresh-indicator"> 正在刷新... </view>
    <pageSkeleton v-if="isLoading"></pageSkeleton>
    <template v-else>
      <!-- 轮播图 -->
      <online-swiper :list="bannerList"></online-swiper>
      <!-- 首页分类 -->
      <Categorypanel :list="categoryList"></Categorypanel>
      <!-- 热门推荐 -->
      <hotPanel :list="hotList"></hotPanel>
      <!-- 猜你喜欢 -->
      <online-guess ref="guessRef"></online-guess>
    </template>
  </scroll-view>
</template>

<script setup lang="ts">
import {
  getHomeBannerAPI,
  getHomeCategoryAPI,
  getHomeHotAPI,
} from '@/services/home';
import { onLoad } from '@dcloudio/uni-app';
import { ref } from 'vue';
import type { BannerItem, categoryItem, hotItem } from '@/types/home';
import CustomNavbar from './components/customNavbar.vue';
import Categorypanel from './components/categorypanel.vue';
import hotPanel from './components/hotPanel.vue';
import pageSkeleton from '@/pages/index/components/pageSkeleton.vue';
import { useGuessList } from '@/composables';

// 获取轮播图数据
const bannerList = ref<BannerItem[]>([]);
const getHomeBannerData = async () => {
  const res = await getHomeBannerAPI();
  bannerList.value = res.result;
};

// 获取首页分类数据
const categoryList = ref<categoryItem[]>([]);
const getHomeCategoryData = async () => {
  const res = await getHomeCategoryAPI();
  categoryList.value = res.result;
};

// 获取热门推荐数据
const hotList = ref<hotItem[]>([]);
const getHotRecommendData = async () => {
  const res = await getHomeHotAPI();
  hotList.value = res.result;
};
// 是否加载中标记
const isLoading = ref(false);
// 页面加载
onLoad(async () => {
  isLoading.value = true;
  await Promise.all([
    getHomeBannerData(),
    getHomeCategoryData(),
    getHotRecommendData(),
  ]);
  isLoading.value = false;
});
const { guessRef, onScrolltolower } = useGuessList();
const isTriggred = ref(false);
// 下拉刷新
const onRefresherrefresh = async () => {
  // 开启动画
  isTriggred.value = true;
  // 加载数据
  // await getHomeBannerData();
  // await getHomeCategoryData();
  // await getHotRecommendData();
  // 重置猜你喜欢组件数据
  guessRef.value?.resetData(); // 加载数据
  await Promise.all([
    getHomeBannerData(),
    getHomeCategoryData(),
    getHotRecommendData(),
    guessRef.value?.getMore(),
  ]);
  // 关闭动画
  isTriggred.value = false;
};
</script>
<style lang="scss">
page {
  background-color: #f7f7f7;
  height: 100vh;
  display: flex;
  flex-direction: column;
}
.scroll-view {
  height: 100vh;
  flex: 1;
  overflow: hidden;
}
.refresh-indicator {
  margin-top: -12px;
  margin-bottom: 10px;
  text-align: center;
  background-color: #f5f5f5;
  color: #999;
  font-size: 14px;
}
</style>
